<?php
include_once ("../Login/databaseconnection.php");
	$Lab_Id=$_POST['Lab_Id'];
   $SchName=$_POST['SchName'];
$name=explode(",",$SchName);
$nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $School_Id=$row['School_Id'];
}
  if($Lab_Id!="Select One"&&$SchName!="")
	   {
$result=mysql_query("SELECT * FROM lab WHERE School_Id='$School_Id' AND Lab_Id='$Lab_Id'");
$resultnet=mysql_query("SELECT * FROM network WHERE School_Id='$School_Id' AND Lab_Id='$Lab_Id'");
$resultres=mysql_query("SELECT * FROM resource_details WHERE School_Id='$School_Id' AND Lab_Id='$Lab_Id'");
	  if($result)
  {
	if($row=mysql_fetch_array($result))
		 {
		 if($resultnet)
		 {
		 if($rownet=mysql_fetch_array($resultnet))
		 {
		 $Availability_Of_LAN=$rownet['Availability_Of_LAN'];
		 $Internet_Facilities=$rownet['Internet_Facilities'];
		 }
		 }
		 $Lab_Type=$row['Lab_Type'];
   $Lab_Id=$row['Lab_Id'];
   $School_Id=$row['School_Id'];
 $Source_Of_Power=$row['Source_Of_Power'];
      	$Tp_Number=$row['Tp_Number'];
    $Account_Number=$row['Account_Number'];
    $Date_of_commencement=$row['Date_of_commencement'];  
	}
	if(mysql_num_rows($result)==0)	  
   {
$_SESSION['errornorow']="Very sorry there is no result,Please check your input and try again!";
   }
   else
   {
   if($_SESSION['Access_Type']=="Administrator")
   {    
   	  $del=mysql_query("DELETE FROM lab WHERE School_Id='$School_Id' AND Lab_Id='$Lab_Id'");		  
	if($del)
  {
$_SESSION['error']="This record is successfully deleted!";
if($resultnet)
   {
		  while($rowtnet=mysql_fetch_array($resultnet))
		 {
		 	$Lab_Id=$rowtnet['Lab_Id'];
			$School_Id=$rowtnet['School_Id'];
		mysql_query("DELETE FROM network WHERE School_Id='$School_Id' AND Lab_Id='$Lab_Id'");
		 }
		 }
		 	 if($resultres)
   {
		 while($rowres=mysql_fetch_array($resultres))
		 {
	    $Lab_Id=$rowres['Lab_Id'];
			$School_Id=$rowres['School_Id'];
		mysql_query("DELETE FROM resource_details WHERE School_Id='$School_Id' AND Lab_Id='$Lab_Id'");
		 }
		 }
 	  if(mysql_num_rows(mysql_query("SELECT * FROM network WHERE School_Id='$School_Id' AND Lab_Id='$Lab_Id'"))!=0)
	  {
$_SESSION['error']=$_SESSION['error']." ,unable to delete this record from network details!";
  }
  
  if(mysql_num_rows(mysql_query("SELECT * FROM resource_details WHERE School_Id='$School_Id' AND Lab_Id='$Lab_Id'"))!=0)
	  {
 $_SESSION['error']=$_SESSION['error']." ,unable to delete this record from resource details!";
   }
}
if(!$del)
  {
 $_SESSION['error']="Sorry unable to delete this lab's basic record from Lab's record!";
  $Lab_Type='';
  $Internet_Facilities='';
   $School_Id='';
 $Source_Of_Power='';
   $Tp_Number='';
    $Account_Number='';
	$Availability_Of_LAN='';
    $Date_of_commencement=''; 
  }
  if(!$delnet)
  {
$_SESSION['error']=$_SESSION['error']." ,unable to delete this record from Lab's network details!";  
  }
  if(!$delres)
  {
$_SESSION['error']=$_SESSION['error']." and unable to delete this Lab's resources!";  
  }
   } 
   else if($_SESSION['Access_Type']=="Teacher in Charge")
   {
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
  if($rowsch=mysql_fetch_array($get_Sch))
{
  $Own_School_Id=$rowsch['School_Id'];
  
   if($School_Id==$Own_School_Id)
	{
	date_default_timezone_set("Europe/Paris");
    $t_d=date("Y/m/d");
	$Accessed_By=$_SESSION['NIC_Number'];
	if($row=mysql_fetch_array($result))
   {
    if($resultnet)
		 {
		 if($rownet=mysql_fetch_array($resultnet))
		 {
		 $Availability_Of_LAN=$rownet['Availability_Of_LAN'];
		 $Internet_Facilities=$rownet['Internet_Facilities'];
		 }
		 }
   $Lab_Type=$row['Lab_Type'];
  $Lab_Id=$row['Lab_Id'];
   $School_Id=$row['School_Id'];
 $Source_Of_Power=$row['Source_Of_Power'];
      	$Tp_Number=$row['Tp_Number'];
    $Account_Number=$row['Account_Number'];
    $Date_of_commencement=$row['Date_of_commencement'];   
	$Last_Updated_Date=$row['Last_Updated_Date'];  
      	$Special_Project=$row['Special_Project'];
      	$Lab_Category=$row['Lab_Category'];
   }
   if($resultnet)
		 {
   while($rownet=mysql_fetch_array($resultnet))
   {
   if(empty($Availability_Of_LAN))
   {
   $Availability_Of_LAN=$rownet['Availability_Of_LAN'];
   }
   else
   {
      $Availability_Of_LAN=$Availability_Of_LAN.",".$rownet['Availability_Of_LAN'];
   }
   if(empty($LAN_Supplier))
   {
   $LAN_Supplier=$rownet['LAN_Supplier'];
   }
   else
   {
      $LAN_Supplier=$LAN_Supplier.",".$rownet['LAN_Supplier'];
   }
   if(empty($Supplied_Date))
   {
   $Supplied_Date=$rownet['Supplied_Date'];
   }
   else
   {
      $Supplied_Date=$Supplied_Date.",".$rownet['Supplied_Date'];
   }
    if(empty($Speed_Of_WAN))
   {
   $Speed_Of_WAN=$rownet['Speed_Of_WAN'];
   }
   else
   {
      $Speed_Of_WAN=$Speed_Of_WAN.",".$rownet['Speed_Of_WAN'];
   }
    if(empty($WAN_Connection_Type))
   {
   $WAN_Connection_Type=$rownet['WAN_Connection_Type'];
   }
   else
   {
      $WAN_Connection_Type=$WAN_Connection_Type.",".$rownet['WAN_Connection_Type'];
   }
    if(empty($Wired_Date))
   {
   $Wired_Date=$rownet['Wired_Date'];
   }
   else
   {
      $Wired_Date=$Wired_Date.",".$rownet['Wired_Date'];
   }
     if(empty($Internet_Facilities))
   {
   $Internet_Facilities=$rownet['Internet_Facilities'];
   }
   else
   {
      $Internet_Facilities=$Internet_Facilities.",".$rownet['Internet_Facilities'];
   }
	     }
		 }
		 if($resultres)
		 {
	 while($rowres=mysql_fetch_array($resultres))
   {
   if(empty($Resource_Id))
   {
   $Resource_Id=$rownet['Resource_Id'];
   }
   else
   {
      $Resource_Id=$Resource_Id.",".$rownet['Resource_Id'];
   }
   if(empty($Serial_Number))
   {
   $Serial_Number=$rownet['Serial_Number'];
   }
   else
   {
      $Serial_Number=$Serial_Number.",".$rownet['Serial_Number'];
   }
   if(empty($Functioning))
   {
   $Functioning=$rownet['Functioning'];
   }
   else
   {
      $Functioning=$Functioning.",".$rownet['Functioning'];
   }
   if(empty($Brand_Name))
   {
   $Brand_Name=$rownet['Brand_Name'];
   }
   else
   {
      $Brand_Name=$Brand_Name.",".$rownet['Brand_Name'];
   }
if(empty($Date_Of_Supplied))
   {
   $Date_Of_Supplied=$rownet['Date_Of_Supplied'];
   }
   else
   {
      $Date_Of_Supplied=$Date_Of_Supplied.",".$rownet['Date_Of_Supplied'];
   }
   if(empty($Name_Of_Supplier))
   {
   $Name_Of_Supplier=$rownet['Name_Of_Supplier'];
   }
   else
   {
      $Name_Of_Supplier=$Name_Of_Supplier.",".$rownet['Name_Of_Supplier'];
   }
   if(empty($Funding_Source))
   {
   $Funding_Source=$rownet['Funding_Source'];
   }
   else
   {
      $Funding_Source=$Funding_Source.",".$rownet['Funding_Source'];
   }   
     }
	 }
   	 $sql=mysql_query("INSERT INTO deleted_lab(Special_Project,Lab_Category,Lab_Id,Lab_Type,School_Id,Source_Of_Power,Tp_Number,Account_Number,Date_of_commencement,Deleted_date,Accessed_By,Last_Updated_Date,Availability_Of_LAN,LAN_Supplier,Speed_Of_WAN,Supplied_Date,WAN_Connection_Type,Wired_Date,Internet_Facilities,Serial_Number,Functioning,Brand_Name,Date_Of_Supplied,Name_Of_Supplier,Funding_Source) VALUES('$Special_Project','$Lab_Category','$Lab_Id','$Lab_Type','$School_Id','$Source_Of_Power','$Tp_Number','$Account_Number','$Date_of_commencement','$t_d','$Accessed_By','$Last_Updated_Date','$Availability_Of_LAN','$LAN_Supplier','$Speed_Of_WAN','$Supplied_Date','$WAN_Connection_Type','$Wired_Date','$Internet_Facilities','$Serial_Number','$Functioning','$Brand_Name','$Date_Of_Supplied','$Name_Of_Supplier','$Funding_Source')");
if($sql){
	   	  $del=mysql_query("DELETE FROM lab WHERE School_Id='$School_Id' AND Lab_Id='$Lab_Id'");
		 
	if($del)
  {
$_SESSION['error']="This record is successfully deleted!";
if($resultnet)
   {
		  while($rowtnet=mysql_fetch_array($resultnet))
		 {
		 	$Lab_Id=$rowtnet['Lab_Id'];
			$School_Id=$rowtnet['School_Id'];
		mysql_query("DELETE FROM network WHERE School_Id='$School_Id' AND Lab_Id='$Lab_Id'");
		 }
		 }
		 	 if($resultres)
   {
		 while($rowres=mysql_fetch_array($resultres))
		 {
	    $Lab_Id=$rowres['Lab_Id'];
			$School_Id=$rowres['School_Id'];
		mysql_query("DELETE FROM resource_details WHERE School_Id='$School_Id' AND Lab_Id='$Lab_Id'");
		 }
		 }
 	  if(mysql_num_rows(mysql_query("SELECT * FROM network WHERE School_Id='$School_Id' AND Lab_Id='$Lab_Id'"))!=0)
	  {
$_SESSION['error']=$_SESSION['error']." ,unable to delete this record from network details!";
  }
  
  if(mysql_num_rows(mysql_query("SELECT * FROM resource_details WHERE School_Id='$School_Id' AND Lab_Id='$Lab_Id'"))!=0)
	  {
 $_SESSION['error']=$_SESSION['error']." ,unable to delete this record from resource details!";
   }
 }
  if(!$del)
  {
  $Availability_Of_LAN='';
  $Internet_Facilities='';
  $Lab_Type='';
   $School_Id='';
 $Source_Of_Power='';
      	$Tp_Number='';
    $Account_Number='';
    $Date_of_commencement='';
 $_SESSION['error']="Sorry unable to delete this lab's basic record from Lab's record!";
 mysql_query("DELETE FROM deleted_lab WHERE School_Id='$School_Id' AND Lab_Id='$Lab_Id' AND Deleted_Date='$t_d' AND Resource_Id='".$Resource_Id."' AND Serial_Number='".$Serial_Number."'");
  }
  if(!$delnet)
  {
 $_SESSION['error']=$_SESSION['error']." and unable to delete this record from Lab's network details!";
}
if(!$delres)
  {
$_SESSION['error']=$_SESSION['error']." and unable to delete this Lab's resources!";  
  }
  }
     if(!$sql)
{
$Internet_Facilities='';
$Availability_Of_LAN='';
	$Lab_Type='';
   $School_Id='';
 $Source_Of_Power='';
      	$Tp_Number='';
    $Account_Number='';
    $Date_of_commencement='';
 $_SESSION['error']="Sorry unable to delete this record!";
 }
  }
else
  {
  $_SESSION['errornot']="Access denied,Sorry you are not allowed!";
 $Lab_Type='';
 $Availability_Of_LAN='';
 $Internet_Facilities='';
   $School_Id='';
 $Source_Of_Power='';
      	$Tp_Number='';
    $Account_Number='';
    $Date_of_commencement=''; 
  }
  }
     	else
  {
  $_SESSION['errornot']="Access denied,Sorry you are not allowed!";
  $Lab_Type='';
  $Internet_Facilities='';
  $Availability_Of_LAN='';
   $School_Id='';
 $Source_Of_Power='';
      	$Tp_Number='';
    $Account_Number='';
    $Date_of_commencement='';
  }
  }
 else
  {
  $_SESSION['errornot2']="Sorry you are not an authorised user!";
  $Lab_Type='';
  $Internet_Facilities='';
  $Availability_Of_LAN='';
   $School_Id='';
 $Source_Of_Power='';
      	$Tp_Number='';
    $Account_Number='';
    $Date_of_commencement=''; 
  }
  }
  }
  }
  if($Lab_Id=="Select One"||$SchName=="")
	   {
	   $_SESSION['errorin']="Not enough inputs to delete!";
	   }
?>
